import {ethers} from "ethers"

const ALCHEMY_MAINNET_URL = 'https://eth-sepolia.public.blastapi.io';

const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

// 利用助记词和provider创建钱包
const wallet1 = ethers.Wallet.fromPhrase('magnet genuine custom runway vessel misery monkey kite chat diary habit decide', provider)
const address = wallet1.getAddress()
console.log(`余额:${ethers.formatEther(await provider.getBalance(wallet1))}`)

// WETH abi
const abiWETH = [
    "function balanceOf(address) public view returns(uint)",
    "function deposit() public payable",
    "function transfer(address, uint) public returns (bool)",
    "function withdraw(uint) public",
];
// WETH合约地址（sepolia 测试网）
const addressWETH = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9' // WETH Contract

// 创建可写合约对象
const contractWETH = new ethers.Contract(addressWETH, abiWETH, wallet1)
// WETH余额
const balanceWETH = await contractWETH.balanceOf(address)
console.log(`WETH余额:${ethers.formatEther(balanceWETH)}`)
// 存入0.001ETH 
const tx = await contractWETH.deposit({value: ethers.parseEther('0.001')})
// 等待交易上链
await tx.wait()
// console.log(tx)

const balanceWETH2 = await contractWETH.balanceOf(address)
console.log(`WETH余额:${ethers.formatEther(balanceWETH2)}`)

// 调用transfer 转账
// const tx2 = await contractWETH.transfer('vitalik.eth', ethers.parseEther('0.001'))
// await tx2.wait()
// const balanceWETH3 = await contractWETH.balanceOf(address)
// console.log(`WETH余额:${ethers.formatEther(balanceWETH3)}`)

// withdraw 

const tx3 = await contractWETH.withdraw(ethers.parseEther('0.001'))
await tx3.wait()
const balanceWETH4 = await contractWETH.balanceOf(address)
console.log(`WETH余额:${ethers.formatEther(balanceWETH4)}`)
const balanceETH  = await provider.getBalance(address)
console.log(`eth余额${ethers.formatEther(balanceETH)}`)
